import {
  call, put, all, takeLatest, select, takeEvery
} from 'redux-saga/effects'
import api from '@API'
import {
  GET_ORDER_DETAIL,
  UPDATE_ORDER_STATUS,
  GET_MY_CONSULTATION_ORDERS,
  GET_MY_SERVICE_PACK_ORDERS, UPDATE_SERVICE_PACE_ORDER_STATUS
} from '@ACTIONS/order'
import { DEFAULT_TAKE, CONSULTATION_TYPE } from '@CONSTANTS/common'
import { ConsulStateMap, ORDER_ITEM_TYPE_ENUMS } from '@CONSTANTS/order'
import { getServicePackOrders } from '@SAGAS/dataSelect'

const getOrderDetail = function* getOrderDetail(action) {
  try {
    const { orderId, isInit } = action
    const result = yield call(api.getOrderById, {
      oId: orderId,
    })
    yield put({
      type: GET_ORDER_DETAIL.SUCCESS,
      orderDetail: result,
    })
    if (isInit) return
    const isServicePack = result.ItemType === ORDER_ITEM_TYPE_ENUMS.ServicePack
    if (isServicePack) {
      yield put({
        type: UPDATE_SERVICE_PACE_ORDER_STATUS.REQUEST,
        data: { orderId, orderDetail: result },
      })
    } else {
      yield put({
        type: UPDATE_ORDER_STATUS.REQUEST,
        data: { orderId, status: result.Status, reason: result?.Consultation?.Reason },
      })
    }
  } catch (error) {
    yield put({ type: GET_ORDER_DETAIL.FAILURE, error })
  }
}

const getMyConsultationOrders = function* getMyConsultationOrders(action) {
  const { payload } = action
  const { skip, type } = payload || {}
  try {
    if (!Object.values(CONSULTATION_TYPE).includes(type)) return

    const {
      data, canLoad, getPreList, loading, initLoad
    } = ConsulStateMap[type]
    const preList = yield select(getPreList)
    const realSkip = skip === 0 ? 0 : preList?.length
    const { Orders } = yield api.getMyConsultationOrders({ skip: realSkip, take: DEFAULT_TAKE, type })

    yield put({
      type: GET_MY_CONSULTATION_ORDERS.SUCCESS,
      [data]: skip === 0 ? Orders : [...preList, ...Orders],
      [canLoad]: Orders.length >= DEFAULT_TAKE,
      [loading]: false,
      [initLoad]: false
    })
  } catch (error) {
    yield put({ type: GET_MY_CONSULTATION_ORDERS.FAILURE, error, currType: type })
  }
}

const getMyServicePackOrders = function* getMyServicePackOrders(action) {
  const { payload } = action
  const { skip, type } = payload || {}
  try {
    const preList = yield select(getServicePackOrders)
    const realSkip = skip === 0 ? 0 : preList?.length
    const { Orders } = yield api.getMyServicePackOrders({ skip: realSkip, take: DEFAULT_TAKE, type })

    yield put({
      type: GET_MY_SERVICE_PACK_ORDERS.SUCCESS,
      servicePackOrders: skip === 0 ? Orders : [...preList, ...Orders],
      canLoadServicePackOrders: Orders.length >= DEFAULT_TAKE,
      loadingServicePackOrders: false,
      initLoadServicePackOrders: false
    })
  } catch (error) {
    yield put({ type: GET_MY_SERVICE_PACK_ORDERS.FAILURE, error, currType: type })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(GET_ORDER_DETAIL.REQUEST, getOrderDetail),
    takeEvery(GET_MY_CONSULTATION_ORDERS.REQUEST, getMyConsultationOrders),
    takeEvery(GET_MY_SERVICE_PACK_ORDERS.REQUEST, getMyServicePackOrders),
  ])
}
